package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

//套餐
@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Override
    public Page<Setmeal> findByPage(Integer pageNum, Integer pageSize, String name) {
        //1.根据name查询套餐
        //1.1设置分页条件
        Page<Setmeal> page = new Page<>(pageNum,pageSize);
        //1.2设置业务条件
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        //1.3执行查询
        page=setmealMapper.selectPage(page,wrapper);
        //2.遍历列表
        List<Setmeal> setmealList = page.getRecords();
        if (CollectionUtil.isNotEmpty(setmealList)) {
            for (Setmeal setmeal : setmealList) {
                //3.根据category_id查询分类名
                Category category = categoryMapper.selectById(setmeal.getCategoryId());
                setmeal.setCategoryName(category.getName());
//                //4.根据id查询set_dish表
//                LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
//                wrapper1.eq(SetmealDish::getSetmealId,setmeal.getId());
//                List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper1);
//                setmeal.setSetmealDishes(setmealDishList);
            }
        }
        return page;
    }
    //新增
    @Override
    public void save(Setmeal setmeal) {
        //1.将套餐基本信息存入dish，返回id
        setmealMapper.insert(setmeal);
        //遍历
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        if(CollectionUtil.isNotEmpty(setmealDishList)) {
            int i = 0;
            for (SetmealDish setmealDish : setmealDishList) {
                //2.设置中间表的setmeal_id，sort
                setmealDish.setSetmealId(setmeal.getId());
                setmealDish.setSort(i++);
                //3.将每个信息都存入中间表
                setmealDishMapper.insert(setmealDish);
            }
        }
    }
    //批量删除/删除
    @Override
    public void deleteByIds(List<Long> ids) {
        //1.判断是否由在售状态的套餐
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getStatus,1).in(Setmeal::getId,ids);
        if (setmealMapper.selectCount(wrapper) > 0) {
            throw new CustomException("有在售卖状态的套餐，无法删除");
        }
        //2.先调用mapper删除中间表的相关信息
        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(SetmealDish::getSetmealId,ids);
        setmealDishMapper.delete(wrapper1);
        //3.再删除套餐信息
        setmealMapper.deleteBatchIds(ids);


    }

    @Override
    public Setmeal findById(Long id) {
        //1.根据id查询套餐表
        Setmeal setmeal = setmealMapper.selectById(id);
        //2.根据category_id查询分类表中分类名
        Category category = categoryMapper.selectById(setmeal.getCategoryId());
        setmeal.setCategoryName(category.getName());
        //3.根据id查询中间表
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);
        setmeal.setSetmealDishes(setmealDishList);
        return setmeal;
    }
    //修改
    @Override
    public void update(Setmeal setmeal) {
        //1.根据id更新套餐
        setmealMapper.updateById(setmeal);
        //2.根据套餐id删除中间表的关联信息
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        setmealDishMapper.delete(wrapper);
        //3.重新添加新的中间表关联信息
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishList)) {
            int i=0;
            for (SetmealDish setmealDish : setmealDishList) {
                setmealDish.setSetmealId(setmeal.getId());
                setmealDish.setSort(i++);
                setmealDishMapper.insert(setmealDish);
            }
        }
    }
    //起售/停售
    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        //设置状态
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        //设置条件：在ids范围的
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId,ids);
        //修改
        setmealMapper.update(setmeal,wrapper);
    }
    //指定分类下的套餐列表
    @Override
    public List<Setmeal> findList(Long categoryId, Integer status) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId,categoryId).eq(Setmeal::getStatus,status);
        List<Setmeal> setmealList = setmealMapper.selectList(wrapper);
        return setmealList;
    }
    //查询指定套餐下的菜品列表
    @Override
    public List<Dish> findDishList(Long id) {
        //先查询dishList
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);
        List<Dish> dishList = new ArrayList<Dish>();
        if (CollectionUtil.isNotEmpty(setmealDishList)) {
            //遍历查询每个菜品的数量
            for (SetmealDish setmealDish : setmealDishList) {
                Dish dish = dishMapper.selectById(setmealDish.getDishId());
                dish.setCopies(setmealDish.getCopies());
                dishList.add(dish);
            }
        }
        return dishList;
    }
}
